package com.blockmeta.bbs.immodule.adapter;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.ToggleButton;

import com.blockmeta.bbs.baselibrary.base.adapter.BaseAdapter;
import com.blockmeta.bbs.baselibrary.utils.LoadPicUtil;
import com.blockmeta.bbs.businesslibrary.widget.CircleImageView;
import com.blockmeta.bbs.immodule.R;
import com.blockmeta.bbs.immodule.widget.CircleMembersLetterItemDecoration;
import com.blockmeta.bbs.immodule.widget.chatboard.InputFieldQueryItem;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;


public class CircleMemberSelectedAdapter extends BaseAdapter<InputFieldQueryItem, BaseViewHolder> {
    private CircleMembersLetterItemDecoration mDecoration;

    private boolean editable = false;
    private boolean chooseSingle = false;
    private int mSelectedPos = -1;
    private SparseBooleanArray mSelectedPositions = new SparseBooleanArray();

    /**
     * 设置可编辑状态 同步修改装饰器里的的编辑状态来改变分割线长度
     * @param editable 可编辑状态
     */
    public void setEditable(boolean editable, boolean chooseSingle) {
        this.editable = editable;
        this.chooseSingle = chooseSingle;
        mSelectedPos = -1;
        mDecoration.setEditable(editable);
        notifyDataSetChanged();
    }

    public List<InputFieldQueryItem> getCheckedItems() {
        ArrayList<InputFieldQueryItem> selectList = new ArrayList<>();
        int size = mSelectedPositions.size();
        for (int i = 0; i < size; i++) {
            if(mSelectedPositions.valueAt(i)){
                selectList.add(getItem(mSelectedPositions.keyAt(i)));
            }
        }
        return selectList;
    }

    public void clearSelect(){
        mSelectedPositions.clear();
        notifyDataSetChanged();
    }

    //设置给定位置条目的选择状态
    private void setItemChecked(int position, boolean isChecked) {
        mSelectedPositions.put(position, isChecked);
    }

    //根据位置判断条目是否选中
    private boolean isItemChecked(int position) {
        return mSelectedPositions.get(position);
    }

    public CircleMemberSelectedAdapter(int layoutResId, @Nullable List<InputFieldQueryItem> data) {
        super(layoutResId, data);
    }

    @Override
    public void bindToRecyclerView(RecyclerView recyclerView) {
        super.bindToRecyclerView(recyclerView);
        mDecoration = new CircleMembersLetterItemDecoration(this);
        recyclerView.addItemDecoration(mDecoration);
    }

    @Override
    protected void convert(BaseViewHolder helper, InputFieldQueryItem item) {
        CircleImageView avatar = helper.getView(R.id.contacts_avatar);

        if(avatar.getTag() == null || !avatar.getTag().toString().equals(item.getImgUrl())){
            LoadPicUtil.loadForIm(avatar, item.getImgUrl());
            avatar.setTag(item.getImgUrl());
        }
        helper.setText(R.id.contacts_name, item.getTitle());

        helper.setText(R.id.contacts_grade,"");
        helper.setText(R.id.contacts_registration_time, "");
        helper.setGone(R.id.iv_icon, false);

        ToggleButton toggleButton = helper.getView(R.id.tg_selector);
        toggleButton.setVisibility(editable? View.VISIBLE:View.GONE);
        toggleButton.setChecked(isItemChecked(helper.getClickPosition()));
        toggleButton.setClickable(false);
        toggleButton.setLongClickable(false);

    }


    public void checkChange(int position) {
        if(chooseSingle){
            if (mSelectedPos == position) {
                setItemChecked(position, false);
                mSelectedPos = -1;
                notifyItemChanged(position + getHeaderLayoutCount());
            } else if (mSelectedPos != position && mSelectedPos != -1) {
                //先取消上个item的勾选状态
                setItemChecked(mSelectedPos, false);
                notifyItemChanged(mSelectedPos + getHeaderLayoutCount());
                //设置新Item的勾选状态
                mSelectedPos = position;
                setItemChecked(mSelectedPos, true);
                notifyItemChanged(position + getHeaderLayoutCount());
            } else if (mSelectedPos == -1) {
                mSelectedPos = position;
                setItemChecked(mSelectedPos, true);
                notifyItemChanged(position + getHeaderLayoutCount());
            }
        }else {
            setItemChecked(position, !isItemChecked(position));
            notifyItemChanged(position + getHeaderLayoutCount());
        }
    }

    /**
     * 滚动到索引位置
     * @param letter 首字母
     */
    public void scrollToTargetLetter(String letter){
       int position = mDecoration.getLetterPosition(letter);
       if (position==-1){return;}
       getRecyclerView().smoothScrollToPosition(position+getHeaderLayoutCount());
    }

    public int getSelectedPos() {
        return mSelectedPos;
    }
}
